package suncertify.network;

import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;

import suncertify.ui.Subscriber;

/**
 * A helper class that opens methods for the controllers to get and set the remote DB
 * @author Paul Wilhelm
 *
 */
public class ServerHelper {
	private static ServerInterface remoteDatabase;			//The remote database to be used
	
	/**
	 * Sets the remote database for the application to use.
	 * @param host - The host name of the remote DB
	 * @param port - The port number of the remote DB
	 * @throws RemoteException
	 * @throws NotBoundException
	 */
	public static void setRemoteDatabase(String host, int port) throws RemoteException, NotBoundException {
		Registry registry = LocateRegistry.getRegistry(host, port);
		remoteDatabase = (ServerInterface) registry.lookup("urlyServer");
	}
	
	/**
	 * Gets the currently selected remote database for the application to use.
	 * @return The currently set remote database
	 */
	public static ServerInterface getRemoteDatabase(){
		return remoteDatabase;
	}
	
	/**
	 * Takes a regular subscriber object and converts it into a remote subscriber object
	 * to be used with RMI.
	 * @param subscriber - The subscriber to convert into a remote subscriber
	 * @return The newly create remote Subscriber object to use with RMI.
	 * @throws RemoteException
	 */
	public static Subscriber createRemoteSubscriber(Subscriber subscriber) throws RemoteException{
		 Subscriber remoteSubscriber = (Subscriber) UnicastRemoteObject.exportObject(subscriber,0);
		 return remoteSubscriber;
	}

}
